*T14.1.2.1;
*T14.1.2.2;
*T14.1.2.3; 
*T14.1.2.4; 
*T14.1.2.5; 
*T14.1.2.6; 

*Start program;

%macro prnt (program=, disname=, ds_list=, popfl=, tpop=, title=, wwwww=, num=);   

%get_data(lst=adsl,pre=adamw,lib=adamw,supp=y);

*Look at Planned treatment ARMCD to derive population in each group;

data adamwadsl(keep=subjid SAFFL enrlfl COMPLFL randafl randbfl PKFL PDFL arma armb armcd actarmcd race racen age ageu sex sexn height weight bmi ethnic ethnicn randafl randbfl); 
set adamwadsl;
length arma armb $4. ;
arma=strip(substr(armcd,1,3));
if index(armcd,"/") then armb=strip(substr(armcd,5));
/*arma=strip(substr(actarmcd,1,3));*/
/*if index(actarmcd,"/") then armb=strip(substr(actarmcd,5));*/
else armb=" ";
if length(arma)<3 then arma=" ";
if length(armb)<3 then armb=" ";
if arma ne " " then arma="A"||strip(arma);
if armb ne " " then armb="B"||strip(armb);
	if arma="" and armb="" then arma="OVER";
			else arma=strip(arma);
/*if index(actarmcd,"&prt")>0 and &popfl then output; */
/*if randafl="Y" then output; */
/*if saffl="Y" then output;*/
if &popfl then output;
run;

*Create separate records for each part below;

data adamwadsld; 
set adamwadsl;
output; output;
run;

proc sort data=adamwadsld; by subjid arma; where arma ne " "; run;

data adamwadsld(drop=arma armb);
length arm $5. column 8.;
set adamwadsld;
by subjid arma;
if first.subjid then arm=arma; 
	else arm=armb;
if arm="AABC" then column=1;
	else if arm="ABCA" then column=2;
	else if arm="ACAB" then column=3;
	else if arm="BABC" then column=4;
	else if arm="BBCA" then column=5;
	else if arm="BCAB" then column=6;
		else if arm="OVER" then column=7;
			else column=.;
	if arm=" " then delete;
output;

run;

*Create additional record OVERALL below;

proc sort data=adamwadsld out=adamwadsld; by subjid arm column; run;

data adamwadslo; set adamwadsld; by subjid; if first.subjid; length arm $5. column 8.; column=7; arm="OVER"; run;

data adamwadsls; set adamwadslo adamwadsld; run;

proc sort data=adamwadsls nodupkey out=adamwadsls1; by _all_; run;

proc sort data=adamwadsls1 out=adamwadsls1; by subjid column; run;

data temporary _pop pop; set adamwadsls1;  usubjid=subjid; by subjid; run;

proc sql;
 select count(distinct usubjid)
 into :doit
 from _pop;
quit;

*******************************************************************************************************
Define headers for each columns
******************************************************************************************************;

  %if &doit > 0 %then %do;


%macro ppp;
%popu(datain=_POP);

%put 

***[&maxtrt - pop1=&pop1]***;

%do i = 1 %to &maxtrt;
   %put pop&i=&&&pop&i;
%end; 

%mend ppp;
%ppp;

proc sort data=temporary out=temporary; by column; run;

*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Age (years)"
                2 = "Gender [n(%)]"
				3 = "Ethnicity [n(%)]"
				4 = "Race [n(%)]"          
                5 = "Weight (kg)"
                6 = "Height (cm)"
				7 = "Body Mass Index (kg/m{\super\ 2})"

                ;

value fracea    1 = "WHITE"
                5 = "BLACK OR AFRICAN AMERICAN"
                3 = "ASIAN"
                4 = "AMERICAN INDIAN OR ALASKA NATIVE"          
                2 = "NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER"
                6 = "OTHER"
				;
value fsexa    1 = "MALE"
               2 = "FEMALE"
				;
value _ftrt     1 = "ABC"
                2 = "BCA"
			    3 = "CAB"
			    4 = "ABC"
				5 = "BCA"
			    6 = "CAB"
			    7 = "Overall "
				;
run;


*Create columns macros;
	proc sql noprint;
	  select  count(distinct(column))
	  into :nbr_report_column
	  from (select distinct column from _pop)
	    group by column;
	quit;

%heads(fmt=_ftrt);

data _heads;
set _heads;
/*header1="~"||strip(header1);*/
/*lbl1="~"||strip(lbl1);*/
/*header2="Dose THC~"||strip(header2);*/
/*lbl2="Dose THC~"||strip(lbl2);*/
/*header3="~"||strip(header3);*/
/*lbl3="~"||strip(lbl3);*/
/*header4=strip(header4)||"~";*/
/*lbl4=strip(lbl4)||"~";*/
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;
* Age (Years) *;
%univariate1age(var=age,
      out=_tmp1,
      width=3,
	decimal=0,
      level1=1,
      level2=,
      level3=,
      where=);

data _tmp1;
 set _tmp1;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, stat1_.);
run;

* Gender *;
%freq(var=SEXN,
      nbvar=2,
      width=3,
      out=_tmp2,
      level1=2,
      level2=,
      level3=,
      where=(sexn NE .));

data _tmp2;
 set _tmp2;
 by level1 level2;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, fsexa.);
run;

* Ethnicity *;
%freq(var=ETHNICN,
      nbvar=2,
      width=3,
      out=_tmp3,
      level1=3,
      level2=,
      level3=,
      where=(ETHNICN NE .));

data _tmp3;
 set _tmp3;
 by level1 level2;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, feth.);
run;

* race *;
%freq(var=RACEN,
      nbvar=6,
      width=3,
      out=_tmp4,
      level1=4,
      level2=,
      level3=,
      where=(RACEN NE .));

data _tmp4;
 set _tmp4;
 by level1 level2;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, fracea.);
run;

*  Weight *;
%univariate1b(var=weight,
      out=_tmp5,
      width=3,
      decimal=1,
      level1=5,
      level2=,
      level3=,
      where=);

data _tmp5;
 set _tmp5;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, stat1_.);
run;

* Height *;
%univariate1b(var=height,
      out=_tmp6,
      width=3,
      decimal=1,
      level1=6,
      level2=,
      level3=,
      where=);

data _tmp6;
 set _tmp6;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, stat1_.);
run;

* BMI *;
%univariate1b(var=bmi,
      out=_tmp7,
      width=3,
      decimal=1,
	  level1=7,
      level2=,
      level3=,
      where=);

data _tmp7;
 set _tmp7;
 length level1_label level2_label $200;
 level1_label = put(level1, level1l.);
 level2_label = put(level2, stat1_.);
run;

*******************************************************************************************************
Create report dataset
******************************************************************************************************;
data report;
 set _tmp1 - _tmp7;
 if 1<=level1<=7 then page =1;
 *else if 7<=level1<=11 then page =2;
 *else if 12<=level1<=16 then page =3;
 *else page =4;
/* if LEVEL1_LABEL=:"Race" and col1="  0" then delete;*/
/* if level2=4 and level1_label="Age (years)" then col1=compress(col1,".0");*/
run;

*To align consisency in race based on CRF;

data report;
set report;
if LEVEL2_LABEL="NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER" then level2=5;
else if LEVEL2_LABEL="BLACK OR AFRICAN AMERICAN" then level2=2;
else level2=level2;
run;

proc contents data=report out=reporta;
run;
%end;

%if &doit=0 %then %do;
	data report;
	page=.; level1=" "; level1_label=" "; level2=" "; level2_label=" "; col1=" "; col2=" "; col3=" "; col4=" "; output;
	run;
%end;

%let doit=&doit;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..DEMOG.rtf";

%open_rtf;

%empty_ds_fix(report);

title4 "Summary of Demographic Characteristics";
title5 "(&title)";
*footnote3 j=l "Note: The safety, PK and PD populations all contain the same set of patients.";

proc report data=report split='~' missing style(header)=[background=white  borderbottomcolor=white bordertopcolor=black  background=_undef_ ] nowd;
 column page level1 level1_label level2 level2_label &wwwww;
/* column page level1 level1_label level2 level2_label ("\brdrb\brdrs\brdrw11  Dose THC" col1 col2 col3 ) col4;*/
/*&columnz;*/
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level1_label / "&linebot. " order order=internal style(column)=[cellwidth=1.6in just=left] style(header)=[just=left];
 define level2       / "&linebot. " order order=internal noprint;
 define level2_label / "&linebot. " order order=internal style(column)=[cellwidth=2.2in just=left] style(header)=[just=left];

/* %do j = 1 %to 3;*/
/*  define &&column&j / "&linebot. &&header&j." style(column)=[cellwidth=1.35in just=center] style(header)=[just=center];*/
/*%end;*/

/*%if %sysfunc(exist(col4))=0 %then %do j = 4 %to 4;*/
/*%end;*/

/*%do j = 4 %to 4;*/
/*	define &&column&j / "&linebot. &&header&j." order order=internal style(column)=[cellwidth=1.35in just=center] style(header)=[just=center];*/
/*%end;*/

  %if &doit > 0 %then %do;

 %do j = 1 %to &nbr_report_column;
  define &&column&j / "&linebot. &&header&j." style(column)=[cellwidth=0.85in just=center] style(header)=[just=center];
 %end;

%end;

 break after page / page;

 compute before level1 / style=[cellwidth=6in just=left];
  line " ";
 endcomp;

/*%if &doit EQ 0 %then %do;*/
/*  define col1 / "&linebot. ~25 mg~(N=&doit)" style(column)=[cellwidth=1.35in just=center] style(header)=[just=left];*/
/*  define col2 / "&linebot. ~25 mg - 2 doses~(N=&doit)" style(column)=[cellwidth=1.35in just=center] style(header)=[just=left];*/
/*  define col3 / "&linebot. ~25 mg - 3 doses~(N=&doit)" style(column)=[cellwidth=1.35in just=center] style(header)=[just=left];*/
/*  define col4 / "&linebot. ~Placebo~(N=&doit)" style(column)=[cellwidth=1.35in just=center] style(header)=[just=left];*/
/**/
/*compute after page/style=[cellwidth=9.7in just=left];*/
/*line "No &tpop on Part &prt where &popfl.Y ";*/
/*endcomp;*/
/**/
/*%end;*/
run;

%close_rtf;
%arrange_rtf;

%mend prnt;

*Call prnt macro;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.1, ds_list=ADSL, popfl=%str(RANDAFL="Y"), tpop=%str(Randomized Population [Part A]), 
      title=%str(Randomized Population [Part A]),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.2, ds_list=ADSL, popfl=%str(RANDBFL="Y"), tpop=%str(Randomized Population [Part B]), 
      title=%str(Randomized Population [Part B]),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.3, ds_list=ADSL, popfl=%str(SAFFL="Y"), tpop=%str(Safety Population), 
      title=%str(Safety Population),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.4, ds_list=ADSL, popfl=%str(PDFL="Y"), tpop=%str(Pharmacodynamic Population), 
      title=%str(Pharmacodynamic Population),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.5, ds_list=ADSL, popfl=%str(PKFL="Y"), tpop=%str(Pharmacokinetic Population), 
      title=%str(Pharmacokinetic Population),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;

%prnt(program=T14_01_02_0X_DEM, disname=14.1.2.6, ds_list=ADSL, popfl=%str(COMPLFL="Y"), tpop=%str(Completer Population), 
      title=%str(Completer Population),wwwww=%str(("\brdrb\brdrs\brdrw11  Part A" col1 col2 col3 ) ("\brdrb\brdrs\brdrw11  Part B" col4 col5 col6 ) col7));
%clrw;









